import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {DomSanitizer} from "@angular/platform-browser";
import swal, { SweetAlertType } from 'sweetalert2';

@Component({
  selector: 'app-c-web-tool',
  templateUrl: './c-web-tool.component.html',
  styleUrls: ['./c-web-tool.component.less']
})
export class CWebToolComponent implements OnInit {
    @Output() onChange = new EventEmitter<any>();
    @Output() onChangeTxt = new EventEmitter<any>();
    @Output() onDelete = new EventEmitter<any>();
    _rightSrc;
    @Input() rightData;
    set rightSrc(value){
        this._rightSrc = value;
    }

    get rightSrc() {
        return this._rightSrc;
    }

    _rightStyleX;
    @Input()
    set rightStyleX(value){
        this._rightStyleX = value;
        this.emitXYWH();
    }

    get rightStyleX(){
        return this._rightStyleX;
    }

    _rightStyleY;
    @Input()
    set rightStyleY(value){
        this._rightStyleY = value;
        this.emitXYWH();
    }

    get rightStyleY(){
        return this._rightStyleY;
    }

    _rightStyleW;
    @Input()
    set rightStyleW(value){
        this._rightStyleW = value;
        this.emitXYWH();
    }

    get rightStyleW(){
        return this._rightStyleW;
    }

    _rightStyleH;
    @Input()
    set rightStyleH(value){
        this._rightStyleH = value;
        this.emitXYWH();
    }

    get rightStyleH(){
        return this._rightStyleH;
    }



    constructor(public sanitizer: DomSanitizer) { }

    ngOnInit() {
        if(!this._rightSrc){
            this.rightSrc = 'http://www.baidu.com'
            this.rightData.src = this.sanitizer.bypassSecurityTrustResourceUrl('http://www.baidu.com');
        }
    }

    emitXYWH() {
        this.onChange.emit({
            x: this._rightStyleX,
            y: this._rightStyleY,
            width: this._rightStyleW,
            height: this._rightStyleH
        });
    }

    onBlur($event) {
        this.rightData.src = this.sanitizer.bypassSecurityTrustResourceUrl(this._rightSrc);
    }

    deleteComponent() {
        this.onDelete.emit();
    }

    save() {
        swal(`保存成功`, '', 'info');
    }
}
